<template>
  <a-spin :spinning="confirmLoading" >
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="24">
          <a-col >
            <a-form-model-item label="年级" prop="gradeId">
              <s-dict-select-tag   v-model="model.gradeId" @change='changeGradeId' placeholder="请选择所属年级" dictCode="cou_grade,grade_name,id"
                                   :is-pin-yin="false"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter='24' style='height: 140px'>
          <a-col>
            <a-form-model-item label='班级' prop='model.classId'>
              <s-dict-select-tag :isPinYin="false" placeholder='请选择班级' v-model='model.classId'
                                  :dictCode='this.queryParamSelect.classIdSelect' />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
<!--            <a-form-model-item label="班级名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">-->
<!--              <a-select v-model="model.classId" placeholder="请选择班级"-->
<!--                        :getPopupContainer="(target) => target.parentNode">-->
<!--                <a-select-option v-for="item in classDataList" :key="item.value">{{ item.label }}</a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-model-item>-->

<!--            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradeId">-->
<!--              <s-dict-select-tag type="list" v-model="model.classId"-->
<!--                                 :dictCode="queryParamSelect.classIdSelect"-->
<!--                                 placeholder="班级"-->
<!--                                 :is-pin-yin="false"/>-->
<!--            </a-form-model-item>-->

          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="教室名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomId">-->
<!--              <a-select v-model="model.classroomId" placeholder="请选择教室"-->
<!--                        :getPopupContainer="(target) => target.parentNode">-->
<!--                <a-select-option v-for="item in classroomDataList" :key="item.value">{{ item.label }}</a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->


        </a-row>
        <a-row :gutter="24">
          <a-col >
            <a-form-model-item label="教学楼" prop="buildingId">
              <s-dict-select-tag @change='changeBuildingId' v-model="model.buildingId" placeholder="选择教学楼" dictCode="fac_building,building_name,id" style='width: 100%' :is-pin-yin="false"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row style='height: 260px'>
          <a-col >
            <a-form-model-item label="教室" prop="classroomId" >

              <s-dict-select-tag v-model='model.classroomId' placeholder='请选择教室'
                                  :dictCode="queryParamSelect.classroomIdSelect" :is-pin-yin="false"/>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { getAllClass,getClassroomDataList } from '@api/commonAPI'

  export default {
    name: 'RelClassClassroomForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        queryParamSelect:{
          classroomIdSelect: null, //'fac_classroom fc ,fc.classroom_name ,fc.id, fc.classroom_iz_open =1 order by fc.building_id !! fc.classroom_order',
          ignorePropertyList: [
            {label: '学年', value: '1'},
            {label: '学期', value: '2'},
            {label: '部门', value: '3'},
            {label: '届级', value: '4'}
          ],
          classIdSelect: null //'cou_class cc,cc.class_name,cc.id ,cc.class_iz_delete =0 and cc.class_iz_open  =1 order by cc.grade_code !! cc.class_order !! cc.class_number'
        },
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/relclassclassroom/relClassClassroom/add",
          edit: "/relclassclassroom/relClassClassroom/edit",
          queryById: "/relclassclassroom/relClassClassroom/queryById"
        },
        //获取下拉班级
        classDataList: [],
        //教室下拉
        classroomDataList: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getClassData();
      this.getClassroomData();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      async getClassData() {
        let res = await getAllClass()
        this.classDataList = []
        res.result.forEach(item => {
          this.classDataList.push({
            label: item.className,
            value: item.id
          })
        })
      },
      async getClassroomData() {
        let res = await getClassroomDataList()
        this.classroomDataList = []
        res.result.forEach(item => {
          this.classroomDataList.push({
            label: item.classroomName,
            value: item.id
          })
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      changeGradeId() {
        if (this.model.gradeId != null && this.model.gradeId != '') {
          this.queryParamSelect.classIdSelect = 'cou_class cc,cc.class_name,cc.id,cc.grade_code =\'' + this.model.gradeId + '\' and cc.class_iz_delete =0 and cc.class_iz_open  =1 order by cc.grade_code !! cc.class_order !! cc.class_number'
        }
      },
      //更改教学楼-->选择教室
      changeBuildingId(){
        if(this.model.buildingId != null || this.model.buildingId != ''){ //判断不为空
          this.queryParamSelect.classroomIdSelect = "fac_classroom fc,fc.classroom_name,fc.id ,fc.building_id in ( \'"+ this.model.buildingId.replaceAll(',','\'!!\'') + "\') order by fc.building_id !! fc.classroom_order" ;
        }
        if(this.model.buildingId == null || this.model.buildingId == ''){ //判断不为空
          this.queryParamSelect.classroomIdSelect = "fac_classroom fc,fc.classroom_name,fc.id order by fc.building_id !! fc.classroom_order" ;
        }
      },
    }
  }
</script>